package com.zork.data.generator.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zork.data.generator.mapper.DataMapMapper;
import com.zork.data.generator.mapper.MaintainGroupMapper;
import com.zork.data.generator.mapper.TbSystemMapper;
import com.zork.data.generator.mapper.TopologyInfoMapper;
import com.zork.data.generator.model.DataMap;
import com.zork.data.generator.model.MaintainGroup;
import com.zork.data.generator.model.TbSystem;
import com.zork.data.generator.model.TopologyInfo;
import com.zork.data.generator.service.DataMapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author xiesen
 */
@Service
@Slf4j
public class DataMapServiceImpl extends ServiceImpl<DataMapMapper, DataMap> implements DataMapService {

    @Resource
    private DataMapMapper dataMapMapper;

    @Resource
    private TopologyInfoMapper topologyInfoMapper;

    @Resource
    private TbSystemMapper tbSystemMapper;

    @Resource
    private MaintainGroupMapper maintainGroupMapper;

    @Override
    public int addDataMap(DataMap dataMap) {
        return dataMapMapper.insert(dataMap);
    }

    @Override
    public int syncDataMap() {
        final LambdaQueryWrapper<TopologyInfo> topologyInfoLambdaQueryWrapper = Wrappers.lambdaQuery(TopologyInfo.class)
                .select(TopologyInfo::getTopoid, TopologyInfo::getSystemname);
        final List<TopologyInfo> topologyInfos = topologyInfoMapper.selectList(topologyInfoLambdaQueryWrapper);
        AtomicInteger size = new AtomicInteger(0);
        for (TopologyInfo topologyInfo : topologyInfos) {
            System.out.println(topologyInfo.toString());
            LambdaQueryWrapper<MaintainGroup> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(MaintainGroup::getXTMC, topologyInfo.getSystemname());
            MaintainGroup maintainGroup = null;
            try {
                final List<MaintainGroup> maintainGroups = maintainGroupMapper.selectList(queryWrapper);
                if (maintainGroups.size() > 0) {
                    for (MaintainGroup group : maintainGroups) {
                        if (StrUtil.isNotBlank(group.getXTJC())) {
                            maintainGroup = group;
                            break;
                        }
                    }
                }
                if (null == maintainGroup) {
                    log.warn("通过系统名称[{}]在cmdb维护组中未查询到维护组信息.", topologyInfo.getSystemname());
                    continue;
                }
            } catch (MybatisPlusException e) {
                e.printStackTrace();
                log.error("MybatisPlus 查询cmdb维护组异常.查询系统参数: {}", topologyInfo.getSystemname());
                continue;
            }
            LambdaQueryWrapper<TbSystem> querySystemWrapper = Wrappers.lambdaQuery();
            querySystemWrapper.eq(TbSystem::getCfname, topologyInfo.getSystemname());
            final List<TbSystem> tbSystems = tbSystemMapper.selectList(querySystemWrapper);
            String appDescription = topologyInfo.getSystemname() + " 描述信息";
            String bizPerson = "";
            String keeper = "";
            String standbyKeeper = "";
            if (tbSystems.size() > 0) {
                appDescription = tbSystems.get(0).getCdesc();
                bizPerson = tbSystems.get(0).getBussperson();
                keeper = tbSystems.get(0).getAppSysKeeper();
                standbyKeeper = tbSystems.get(0).getAppStandbySysKeeper();
            }
            final DataMap dataMap = DataMap.builder().appSystem(maintainGroup.getXTJC())
                    .appSystemName(topologyInfo.getSystemname())
                    .appDescription(appDescription)
                    .systemMark(randomSystemMark()).systemLevel(maintainGroup.getXTFWJB())
                    .isShow(1).bizPerson(bizPerson).appSysKeeper(keeper).appStandbySysKeeper(standbyKeeper).build();

            final DataMap srcDataMap = searchByAppSystem(dataMap.getAppSystem());
            if (null == srcDataMap) {
                dataMapMapper.insert(dataMap);
            } else {
                dataMap.setId(srcDataMap.getId());
                dataMapMapper.updateById(dataMap);
            }
            size.getAndIncrement();

            try {
                Thread.sleep(100);
            } catch (InterruptedException ignored) {
            }
        }

        return size.get();
    }

    private int randomSystemMark() {
        Random random = new Random();
        return random.nextInt(41) + 60;
    }

    @Override
    public List<DataMap> searchByAppSystem(List<String> apps) {
        LambdaQueryWrapper<DataMap> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(DataMap::getAppSystemName, apps);
        return dataMapMapper.selectList(queryWrapper);
    }

    @Override
    public DataMap searchByAppSystem(String app) {
        return this.getOne(Wrappers.lambdaQuery(DataMap.class).eq(DataMap::getAppSystem, app));
    }

    @Override
    public DataMap searchByAppSystemName(String appName) {
        return this.getOne(Wrappers.lambdaQuery(DataMap.class).eq(DataMap::getAppSystemName, appName));
    }
}
